package com.irononetech.android.MultilevelListView;

import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleExpandableListAdapter;

import com.irononetech.android.Application.Application;
import com.irononetech.android.formcomponent.FormObject;
import com.irononetech.android.formcomponent.view.VehicleDetailsActivity;
import com.irononetech.android.claimsonersa.R;

public class RadioButtonListView extends ExpandableListActivity {

	Logger LOG = LoggerFactory.getLogger(RadioButtonListView.class);

	GroupClass[] buttonarraylist;
	boolean[] checkstatus;
	Button backCancelBtn;
	final int noOfSubItems = 4;
	
	static final String colors[] = { "FR", "F/L", "R/R/L", "R/L/R", "R/L/L",
			"R/R/R" };

	static final String shades[][] = {
			// Shades of
			{ "Good", "", "Fair", "", "Bold", "", "Not Applicable", "" },
			// Shades of
			{ "Good", "", "Fair", "", "Bold", "", "Not Applicable", "" },
			// Shades of
			{ "Good", "", "Fair", "", "Bold", "", "Not Applicable", "" },
			// Shades of
			{ "Good", "", "Fair", "", "Bold", "", "Not Applicable", "" },
			// Shades of
			{ "Good", "", "Fair", "", "Bold", "", "Not Applicable", "" },
			// Shades of
			{ "Good", "", "Fair", "", "Bold", "", "Not Applicable", "" } };

	FormObject formObject;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle) {
		try {
			LOG.debug("ENTRY onCreate");
			super.onCreate(icicle);
			setContentView(R.layout.radiobutton_main);
			buttonarraylist = new GroupClass[30];

			formObject = Application.getFormObjectInstance();
			checkstatus = formObject.getselectTirecondition();

			backCancelBtn = (Button) findViewById(R.id.button2);
			
			if(formObject.getisSEARCH()){
				backCancelBtn.setBackgroundDrawable(getResources()
						.getDrawable(R.drawable.back_button_xml));
			}else{
				backCancelBtn.setBackgroundDrawable(getResources()
						.getDrawable(R.drawable.cancel_button_xml));
			}
			
			HelpSimpleExpandableListAdapter expAdapter = new HelpSimpleExpandableListAdapter(
					this, createGroupList(), 		// groupData describes the first-level entries
					R.layout.radiobutton_group_row, // Layout for the first-level entries
					new String[] { "colorName" }, 	// Key in the groupData maps to display
					new int[] { R.id.childname }, 	// Data under "colorName" key goes into this TextView
					createChildList(), 				// childData describes second-level entries
					R.layout.radiobutton_child_row, // Layout for second-level entries
					new String[] { "shadeName", "rgb" }, 	// Keys in childData maps to display
					new int[] { R.id.childname, R.id.rgb } 	// Data under the keys above go into these TextViews
			);
			setListAdapter(expAdapter);
			LOG.debug("SUCCESS onCreate");
		} catch (Exception e) {
			 LOG.error("onCreate:10335");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
		}
	}

	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	
	@Override
	protected void onResume() {
		LOG.debug("ENTRY onResume");
		super.onResume();
		LOG.debug("SUCCESS onResume");
	}

	public void onContentChanged() {
		super.onContentChanged();
		
	}

	public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
		try {
			if (formObject.isEditable()) {
				int position = noOfSubItems * (groupPosition) + childPosition;

				GroupClass gc = buttonarraylist[noOfSubItems * (groupPosition)];
				GroupClass gc1 = buttonarraylist[noOfSubItems * (groupPosition) + 1];
				GroupClass gc2 = buttonarraylist[noOfSubItems * (groupPosition) + 2];
				GroupClass gc3 = buttonarraylist[noOfSubItems * (groupPosition) + 3];

				RadioButton rb = gc.rb;
				RadioButton rb1 = gc1.rb;
				RadioButton rb2 = gc2.rb;
				RadioButton rb3 = gc3.rb;

				if (position == noOfSubItems * (groupPosition)) {
					rb.setChecked(true);
					rb1.setChecked(false);
					rb2.setChecked(false);
					rb3.setChecked(false);
					checkstatus[position] = true;
					checkstatus[position + 1] = false;
					checkstatus[position + 2] = false;
					checkstatus[position + 3] = false;
				}
				if (position == noOfSubItems * (groupPosition) + 1) {
					rb.setChecked(false);
					rb1.setChecked(true);
					rb2.setChecked(false);
					rb3.setChecked(false);
					checkstatus[position] = true;
					checkstatus[position - 1] = false;
					checkstatus[position + 1] = false;
					checkstatus[position + 2] = false;
				}
				if (position == noOfSubItems * (groupPosition) + 2) {
					rb.setChecked(false);
					rb1.setChecked(false);
					rb2.setChecked(true);
					rb3.setChecked(false);
					checkstatus[position] = true;
					checkstatus[position - 1] = false;
					checkstatus[position - 2] = false;
					checkstatus[position + 1] = false;
				}
				if (position == noOfSubItems * (groupPosition) + 3) {
					rb.setChecked(false);
					rb1.setChecked(false);
					rb2.setChecked(false);
					rb3.setChecked(true);
					checkstatus[position] = true;
					checkstatus[position - 1] = false;
					checkstatus[position - 2] = false;
					checkstatus[position - 3] = false;
				}
			}
			return false;
		} catch (Exception e) {
			 LOG.error("onChildClick:10336");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
			return false;
		}
	}

	public void onGroupExpand(int groupPosition) {
		
	}

	/**
	 * Creates the group list out of the colors[] array according to the
	 * structure required by SimpleExpandableListAdapter. The resulting List
	 * contains Maps. Each Map contains one entry with key "colorName" and value
	 * of an entry in the colors[] array.
	 */
	private List createGroupList() {
		ArrayList result = new ArrayList();
		for (int i = 0; i < colors.length; ++i) {
			HashMap m = new HashMap();
			m.put("colorName", colors[i]);
			result.add(m);
		}
		return (List) result;
	}

	/**
	 * Creates the child list out of the shades[] array according to the
	 * structure required by SimpleExpandableListAdapter. The resulting List
	 * contains one list for each group. Each such second-level group contains
	 * Maps. Each such Map contains two keys: "shadeName" is the name of the
	 * shade and "rgb" is the RGB value for the shade.
	 */
	private List createChildList() {
		ArrayList result = new ArrayList();
		for (int i = 0; i < shades.length; ++i) {
			// Second-level lists
			ArrayList secList = new ArrayList();
			for (int n = 0; n < shades[i].length; n += 2) {
				HashMap child = new HashMap();
				child.put("shadeName", shades[i][n]);
				child.put("rgb", shades[i][n + 1]);
				secList.add(child);
			}
			result.add(secList);
		}
		return result;
	}

	public void radiobutton_back_button(View v) {
		try {
			if (!formObject.getisSEARCH()) {
				boolean[] a = new boolean[Application.getTyreConditionArrSize()];
				for (int i = 0; i < a.length; i++) {
					if (i % 4 == 0) {
						a[i + 3] = true;
					}
				}
				formObject.setselectTirecondition(a);
			}
			finish();
		} catch (Exception e) {
			 LOG.error("radiobutton_back_button:10337");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
		}
	}

	public void radiobutton_ok_button(View v) {
		try {
			// int countforTruearray=0;
			/*
			 * for(int i=0;i<18;i++){ if(checkstatus[i]){
			 * trueList[countforTruearray]= (i%3)+1; countforTruearray++; } }
			 */
			formObject.setselectTirecondition(checkstatus);

			Intent mIntent = new Intent(RadioButtonListView.this,
					VehicleDetailsActivity.class);
			mIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			startActivity(mIntent);
			finish();
		} catch (Exception e) {
			 LOG.error("radiobutton_ok_button:10338");
			   if(e != null){
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			  }
		}
	}

	public class HelpSimpleExpandableListAdapter extends SimpleExpandableListAdapter {
		Context c;
		int childlayout;
		RadioGroup rg;

		public HelpSimpleExpandableListAdapter(Context context,
				List<? extends Map<String, ?>> groupData, int groupLayout,
				String[] groupFrom, int[] groupTo,
				List<? extends List<? extends Map<String, ?>>> childData,
				int childLayout, String[] childFrom, int[] childTo) {
			super(context, groupData, groupLayout, groupFrom, groupTo,
					childData, childLayout, childFrom, childTo);
			this.c = context;
			childlayout = childLayout;
		}

		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			View view = super.getChildView(groupPosition, childPosition,
					isLastChild, convertView, parent);
			/*
			 * View view=null; if(convertView==null){ view=
			 * LayoutInflater.from(c).inflate(childlayout, parent); }
			 */

			RadioButton rb = (RadioButton) view.findViewById(R.id.radioButton1);
			rb.setChecked(checkstatus[noOfSubItems * (groupPosition) + childPosition]);

			// rg.addView(rb,childPosition);
			GroupClass gc = new GroupClass();
			gc.rb = rb;
			gc.grouprow = groupPosition;
			gc.childrow = childPosition;
			int position = noOfSubItems * (groupPosition) + childPosition;
			buttonarraylist[position] = gc;

			// Log.d( LOG_TAG,
			// "getChildView: groupPosition: "+groupPosition+"; childPosition: "+childPosition+"; v: "+v
			// );
			return view;
		}

		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			View v = super.getGroupView(groupPosition, isExpanded, convertView,
					parent);
			//Log.d(LOG_TAG, "getGroupView: groupPosition: " + groupPosition
					//+ "; isExpanded: " + isExpanded + "; v: " + v);
			return v;
		}

		//private static final String LOG_TAG = "DebugSimpleExpandableListAdapter";
	}

}

class GroupClass {
	RadioButton rb;
	int grouprow;
	int childrow;
}
